﻿using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //GravShips
    public class GravShip : StarShip
    {
        //constructor
        public GravShip(SVGame game, Player owner, int engines, StarSystem origin)
            : base(game, owner, engines, 0, origin)
        {
        }

        //Ship name
        public override string Name
        {
            get
            {
                return "GravShip";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "GravShips can move entire StarSystems, or other cosmic phenomenon like Pulsars and Wormholes.  When a GravShip targets something to move, its effective Engine rating is reduced to 1.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.GravShip;
            }
        }

        //The starsystem currently being dragged      
        public SVMapObject Dragging
        {
            get
            {
                return _dragging;
            }
            set
            {
                _dragging = value;
                Game.SubmitStateChange(StateChange.Dragging, this, value);
            }
        }

        //can target any StarSystem
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.TargetStarSystem:
                case OrderType.TargetMapObject:
                    return true;
                case OrderType.GravRelease:
                    return Dragging != null;
                default:
                    return base.CanExecuteOrder(ot);
            }
        }
        public override bool CanTargetStarSystem(StarSystem target, bool considerAlliance)
        {
            return true;
        } 

        //They slow down when dragging
        public override int EffectiveEngines
        {
            get
            {
                return Dragging == null ? Engines : 1;
            }
        }

        //Start dragging!
        protected override void TargetAdjacentStarSystem(StarSystem system)
        {
            TargetAdjacentMapObject(system);
        }
        protected internal override void TargetAdjacentMapObject(SVMapObject svm)
        {
            Dragging = svm;
            Order = null;
        }

        //stop dragging if the system's no longer adjacent
        protected internal override void CommonPostProcessing()
        {
            if (Dragging != null)
                if (!IsAdjacent(Dragging))
                    Dragging = null;
        }

        //persistence
        protected override string SubClassPersist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.AppendToken(Dragging);
            return base.SubClassPersist() + tok.Result;
        }
        protected override void SubClassParse(SVTokenizer tok)
        {
            base.SubClassParse(tok);
            Dragging = tok.TokenMapObject;
        }

        //Move the starsystem along with the GravShip
        protected internal override void PostMove(int oldX, int oldY)
        {
            if (Dragging != null)
            {
                Dragging.SetLocation(oldX, oldY);
                Game.SubmitStateChange(StateChange.ObjectMoved, Dragging, oldX, oldY, 1);
                if (Game.Host != null)
                    Game.Host.ObjectMoved(Dragging, oldX, oldY, 1);
            }
        }

        //private members
        private SVMapObject _dragging = null;
    }
}
